package me.zhengjie.minbearwcs.dao.outbound.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.dao.outbound.inf.OutboundStockUpRecordDao;
import me.zhengjie.minbearwcs.entity.Mapper.outbound.OutboundStockUpRecordMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.stockUp.OutboundStockUpRecord;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class OutboundStockUpRecordDaoImpl implements OutboundStockUpRecordDao {

    private final OutboundStockUpRecordMapper mapper;

    @Override
    public boolean insert(OutboundStockUpRecord outboundStockUpRecord) {
        return mapper.insert(outboundStockUpRecord) == 1;
    }

    @Override
    public OutboundStockUpRecord selectByStockUpId(String stockUpId) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getStockUpId, stockUpId)
                .one();
    }

    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateDestinationSmallStorageByStockUpId(String stockUpId, String destinationSmallStorage) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getStockUpId, stockUpId)
                .set(OutboundStockUpRecord::getDestinationSmallStorage, destinationSmallStorage)
                .update();
    }

    @Override
    public OutboundStockUpRecord selectByPalletAndDestinationStorage(String pallet, String destinationSmallStorage) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getPalletCode, pallet)
                .eq(OutboundStockUpRecord::getDestinationSmallStorage, destinationSmallStorage)
                .one();
    }

    @Override
    public String selectStartSmallStorageByOutboundOrderAndPallet(String outboundOrder, String pallet) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getOutboundOrderCode, outboundOrder)
                .eq(OutboundStockUpRecord::getPalletCode, pallet)
                .select(OutboundStockUpRecord::getStartSmallStorage)
                .one()
                .getStartSmallStorage();
    }

    @Override
    public String selectOutboundOrderDetailIdByOutboundOrderAndPallet(String outboundOrder, String pallet) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getOutboundOrderCode, outboundOrder)
                .eq(OutboundStockUpRecord::getPalletCode, pallet)
                .select(OutboundStockUpRecord::getOutboundOrderDetailId)
                .one()
                .getOutboundOrderDetailId();
    }

    @Override
    public List<OutboundStockUpRecord> selectByOutboundOrderCode(String outboundOrderCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundStockUpRecord::getOutboundOrderCode, outboundOrderCode)
                .list();
    }
}
